package com.cheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.CodeUtil;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.AcceptInfoEntity;
import com.cheng.data.mapper.AcceptInfoMapper;
import com.cheng.data.vo.AcceptInfoVO;
import com.cheng.data.vo.StatisticsVO;
import com.cheng.service.AcceptInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: ChengJW
 * @date: 2021/11/8 20:57
 * @version: 1.0
 * @description:
 */
@Service
public class AcceptInfoServiceImpl implements AcceptInfoService {

    private final static Logger logger = LoggerFactory.getLogger(AcceptInfoServiceImpl.class);

    @Autowired
    private AcceptInfoMapper acceptInfoMapper;

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: int
     * @date: 2021/9/20
     * @description: 插入 AcceptInfo 信息
     */
    @Override
    public int insertAcceptInfo(AcceptInfoEntity entity) throws CommonException {
        return acceptInfoMapper.insert(entity);
    }

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: java.util.List<com.cheng.data.entity.AcceptInfoEntity>
     * @date: 2021/9/20
     * @description: 查询 AcceptInfo 信息
     */
    @Override
    public List<AcceptInfoEntity> selectAcceptInfo(AcceptInfoEntity entity) throws CommonException {

        QueryWrapper<AcceptInfoEntity> wrapper = new QueryWrapper<>();
        //主键
        wrapper.eq(StringUtil.isNotNull(entity.getAtId()),"at_Id",entity.getAtId());
        //受理号
        wrapper.eq(StringUtil.isNotNull(entity.getAtAcptId()),"at_Acpt_Id",entity.getAtAcptId());
        //业务申请编号
        wrapper.eq(StringUtil.isNotNull(entity.getAtBussId()),"at_Buss_Id",entity.getAtBussId());
        //企业统一社会信用代码
        wrapper.eq(StringUtil.isNotNull(entity.getAtCreditCode()),"at_Credit_Code",entity.getAtCreditCode());
        //wrapper.eq(StringUtil.isNotNull(entity.getAtCode()),"at_Code",entity.getAtCode());
        if ("1".equals(entity.getAtMvCd())){
            wrapper.eq("at_Mv_Cd",CodeUtil.MOVE_STD_1);
        }else {
            wrapper.eq("at_Mv_Cd", CodeUtil.MOVE_STD_0);
        }
        return acceptInfoMapper.selectList(wrapper);
    }

    /**
     * @author: ChengJW
     * @params: [map]
     * @return: java.util.List<com.cheng.data.entity.AcceptInfoEntity>
     * @date: 2021/9/22
     * @description: 根据条件查询 AcceptInfo 信息
     */
    @Override
    public List<AcceptInfoEntity> selectAcceptInfoList(Map<String, Object> map) throws CommonException {
        return acceptInfoMapper.selectAcceptInfoList(map);
    }
    /**
     * @author: ChengJW
     * @params: [map]
     * @return: int
     * @date: 2021/9/23
     * @description: 根据条件修改 acceptInfo 信息
     */
    @Override
    public int updateAcceptInfo(AcceptInfoEntity AcceptInfoEntity) throws CommonException {
        return acceptInfoMapper.updateById(AcceptInfoEntity);
    }

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: int
     * @date: 2021/9/23
     * @description: 根据条件 acceptInfo 信息
     */
    @Override
    public int deleteAcceptInfo(AcceptInfoEntity entity) throws CommonException {

        QueryWrapper<AcceptInfoEntity> wrapper = new QueryWrapper<>();
        //受理号
        wrapper.eq(StringUtil.isNotNull(entity.getAtAcptId()),"at_Acpt_Id",entity.getAtAcptId());
        //主键
        wrapper.eq(StringUtil.isNotNull(entity.getAtId()),"at_Id",entity.getAtId());
        //业务申请编号
        wrapper.eq(StringUtil.isNotNull(entity.getAtBussId()),"at_Buss_Id",entity.getAtBussId());
        wrapper.eq("at_Mv_Cd",CodeUtil.MOVE_STD_0);
        return acceptInfoMapper.delete(wrapper);
    }

    /**
    * @author: ChengJW
    * @params: [acceptInfoVO]
    * @return: void
    * @date: 2021/12/18
    * @description: 根据条件，批量修改受理信息
    */
    @Override
    public void updateBatchAcceptInfo(AcceptInfoVO acceptInfoVO) throws CommonException {
        AcceptInfoEntity entity = new AcceptInfoEntity();
        entity.setAtCreditCode(acceptInfoVO.getAtCreditCode());
        entity.setAtCode(acceptInfoVO.getAtCode());
        entity.setAtMvCd(acceptInfoVO.getAtMvCd());
        List<AcceptInfoEntity> list = selectAcceptInfo(entity);
        if (list != null && list.size() > 0){
            for (AcceptInfoEntity infoEntity : list){
                //修改机关代码
                infoEntity.setAtCode(acceptInfoVO.getAtCode());
                if (CodeUtil.MOVE_STD_1.equals(acceptInfoVO.getAtMvCd())){
                    infoEntity.setAtMvCd(CodeUtil.MOVE_STD_0);
                }else {
                    infoEntity.setAtMvCd(CodeUtil.MOVE_STD_1);
                }
                infoEntity.setUtDt(new Date());
                acceptInfoMapper.updateById(infoEntity);
            }
        }
    }

    /**
     * @author: ChengJW
     * @params: [map]
     * @return: java.util.List<com.cheng.data.vo.StatisticsVO>
     * @date: 2021/12/20
     * @description: 统计受理信息
     */
    @Override
    public List<StatisticsVO> queryStatisticsAcceptList(Map<String, Object> map) throws CommonException {
        if (map == null){
            return null;
        }
        if (CodeUtil.STATISTIC_TYPE.equals(map.get(CodeUtil.STATISTIC_CODE))){
            acceptInfoMapper.queryStatisticsAcceptList(map);
        }
        return acceptInfoMapper.queryStatisticsAcceptListByCode(map);
    }
}
